package com.imooc.SmartChargeStation.demo.controller;

import com.imooc.SmartChargeStation.demo.mqtt.PahoMode.service.impl.ClientServiceImpl;
import com.imooc.SmartChargeStation.demo.mqtt.PahoMode.utils.MqttClientUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * author: zpy
 * description: mqtt(基于Eclipse Paho) web控制器
 * date: 2025/8/18
 */

@RestController
/*
    组合注解：@Controller + @ResponseBody
    @RestController：标识当前类是一个处理器类，用于接收和处理HTTP请求，并返回响应数据
    @ResponseBody：将所有方法的返回值转换成JSON数据，并返回给客户端
 */
public class MqttPahoCtl {

    @Resource
    private ClientServiceImpl clientService;


    /**
     * mqtt客户端发布消息
     * @param topic
     * @param message
     */
    @RequestMapping(value = "/paho/pub")
    public void pahoPub(@RequestParam String topic, @RequestParam String message){
    /*从请求的 URL 参数（如/paho/pub?topic=test&message=hello）中提取topic（MQTT 主题）和message（要发布的消息）*/
        clientService.publish(topic,message);
        clientService.disconnct();
    }

    /**
     * mqtt客户端订阅消息
     * @param topic
     */
    @RequestMapping(value = "/paho/sub")
    public void pahoSub(@RequestParam String topic) {
        clientService.subScribe(topic);
        clientService.disconnct();
    }
}
